package com.atguigu.servlet;

import com.atguigu.bean.Book;
import com.atguigu.bean.Cart;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @ Author:任留辉
 * @ Description:
 * @ Date:Created in 21:13 2021/3/19 0019
 */
public class CartServlet extends BaseServlet {
    private BookService bookService = new BookServiceImpl();

    /**
     * 添加购物项
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void addCartItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数，即添加的图书的id
        String bookId = request.getParameter("bookId");
        //根据图书id查询图书信息
        Book book = bookService.getBookById(Integer.parseInt(bookId));
        //获取session对象
        HttpSession session = request.getSession();
        //从session中获取购物车对象
        Cart cart = (Cart) session.getAttribute("cart");
        //判断cart对象是否为null
        if(cart == null){
            //表示当前操作是第一次向购物车中添加购物项
            cart = new Cart();
        }
        //添加购物项
        cart.addCartItem(book);
        //将购物车对象在session中共享
        session.setAttribute("cart", cart);
        //重定向到来源页面
        //response.sendRedirect(request.getContextPath() + "/");
        String referer = request.getHeader("referer");
        response.sendRedirect(referer);
    }

    /**
     * 删除购物项
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteCartItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数，即要删除的购物项所对应的图书id
        String bookId = request.getParameter("bookId");
        //获取session对象
        HttpSession session = request.getSession();
        //获取session中共享的购物车对象
        Cart cart = (Cart) session.getAttribute("cart");
        //删除购物项
        cart.deleteCartItem(bookId);
        //重定向购物车页面
        response.sendRedirect(request.getContextPath() + "/pages/cart/cart.jsp");
    }

    /**
     * 清空购物车
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void clearCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取session对象
        HttpSession session = request.getSession();
        //获取session中共享的购物车对象
        Cart cart = (Cart) session.getAttribute("cart");
        //删除购物项
        cart.clearCart();
        //重定向购物车页面
        response.sendRedirect(request.getContextPath() + "/pages/cart/cart.jsp");
    }

    /**
     * 更新购物项的数量
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void updateCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数，即要更新数量的购物项所对应的图书的id，以及更新之后的数量
        String bookId = request.getParameter("bookId");
        String countStr = request.getParameter("count");
        //获取session对象
        HttpSession session = request.getSession();
        //获取session中共享的购物车对象
        Cart cart = (Cart) session.getAttribute("cart");
        //更新购物项的数量
        cart.updateCount(bookId, countStr);
        //重定向到来源页面
        response.sendRedirect(request.getHeader("referer"));
    }
}
